package cn.neusoft.edu.his.demo.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.neusoft.edu.his.demo.mapper.DoctorMapper;
import cn.neusoft.edu.his.demo.mapper.DoctorWorkMapper;
import cn.neusoft.edu.his.demo.mapper.RoomMapper;
import cn.neusoft.edu.his.demo.mapper.WorkPlanMapper;
import cn.neusoft.edu.his.demo.po.Doctor;
import cn.neusoft.edu.his.demo.po.FullDoctorWork;
import cn.neusoft.edu.his.demo.po.Room;
import cn.neusoft.edu.his.demo.po.WorkPlan;

@Service
public class WorkPlanService {
	@Autowired
	private WorkPlanMapper workplanMapper;
	@Autowired
	private DoctorWorkMapper doctorworkMapper;
	@Autowired
	private DoctorMapper doctorMapper;
	@Autowired
	private RoomMapper roomMapper;
	public boolean generatePlan(WorkPlan wp) {		
		int result=0;
		for(int i=1;i<=7;i++)
		{
			wp.setWeekday(i+"");
			
			Date date=wp.getDay(); //取时间 
			Calendar   calendar = new GregorianCalendar(); 
			calendar.setTime(date); 
			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动 
			date=calendar.getTime(); //这个时间就是日期往后推一天的结果 
			wp.setDay(date);
			result+=workplanMapper.insert(wp);
			
		}
		
		return result==7;
	}
	

	public int updateByDay(WorkPlan wp) {
		Date d=wp.getDay();
		int year=1900+d.getYear();
		int month=1+d.getMonth();
		int date=d.getDate();
		int res=workplanMapper.updateByDay(year, month, date, wp.getStatus());
		return res;
	}

	public WorkPlan getPlansStatus(Date d) {
		WorkPlan wp=new WorkPlan();
		for(int i=1;i<=7;i++)
		{
			int year=1900+d.getYear();
			int month=1+d.getMonth();
			int date=d.getDate();
			int day=d.getDay();
			WorkPlan w=workplanMapper.selectByDay(year,month,date);
			System.out.println(w);
			if(w==null)
				return null;
			switch(day)
			{
			case 1://星期一
				wp.setMondayDay(d);
				wp.setMondayStatus(w.getStatus());
				break;
			case 2://星期二
				wp.setTuesdayDay(d);
				wp.setTuesdayStatus(w.getStatus());
				break;
			case 3://星期三
				wp.setWednesdayDay(d);
				wp.setWednesdayStatus(w.getStatus());
				break;
			case 4://星期四
				wp.setThursdayDay(d);
				wp.setThursdayStatus(w.getStatus());
				break;
			case 5://星期五
				wp.setFridayDay(d);
				wp.setFridayStatus(w.getStatus());
				break;
			case 6://星期六
				wp.setSaturdayDay(d);
				wp.setSaturdayStatus(w.getStatus());
				break;
			case 7://星期日
				wp.setSundayDay(d);
				wp.setSundayStatus(w.getStatus());
				break;
			}
			Calendar   calendar = new GregorianCalendar(); 
			calendar.setTime(d); 
			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动 
			d=calendar.getTime(); //这个时间就是日期往后推一天的结果 
			
		}
		return wp;
		
	}

	/*
	 * 获得指定日期的医生排班情况
	 * */
	public List<FullDoctorWork> getAllBaseInfo(String day) {
		List<FullDoctorWork> dws=this.doctorworkMapper.selectAllInfo(day);
		List<Doctor> doctors=this.doctorMapper.selectList(null);
		List<Room> rooms=this.roomMapper.selectList(null);
		String[] dwIds=new String[dws.size()];
		for(int i=0;i<dws.size();i++)
			dwIds[i]=dws.get(i).getDocId();
		if(dws.size()<doctors.size())//处理尚未排班的医生
		{
			for(Doctor d:doctors)
			{
				if(!Arrays.asList(dwIds).contains(d.getDocId()))
				{
					FullDoctorWork fdw=new FullDoctorWork();
					fdw.setDocId(d.getDocId());
					fdw.setDocName(d.getDocName());
					fdw.setDocTitle(d.getDocTitle());
					fdw.setDepartId1(d.getDepartId1());
					fdw.setDepartId2(d.getDepartId2());
					
					dws.add(fdw);
				}
			}
		}
		for(FullDoctorWork fdw:dws)
		{
			List<Room> myRoom=new ArrayList<Room>();
			for(Room room:rooms)
			{
				if(fdw.getDepartId1()==room.getDepartId1())
				{
					myRoom.add(room);
				}
			}
			fdw.setRooms(myRoom);
		}
		return dws;
	}
}
	
